﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports Newtonsoft.Json
Imports System
Imports System.Collections.ObjectModel
Imports System.Globalization
Imports System.Text.RegularExpressions

Namespace roncliProductions.LibWowArmory.Calendar.Detail

    ''' <summary>
    ''' A class that defines a calendar event
    ''' </summary>
    ''' <remarks></remarks>
    Public Class CalendarDetail
        Inherits WowArmoryData

        Private cdDetail As New Schema.calendarDetail
        Private iInvitees As New Collection(Of Invitee)

        ''' <summary>
        ''' The realm of the character for the calendar to lookup
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Realm As String

        ''' <summary>
        ''' The name of the character for the calendar to lookup
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Name As String

        ''' <summary>
        ''' The identification number of the event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property EventID As Integer

        ''' <summary>
        ''' The URL that will be used to retrieve the event detail from the armory
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL As String
            Get
                Return String.Format(CultureInfo.InvariantCulture, "http://www.wowarmory.com/vault/calendar/detail.json?callback=calendar.loadEventDetail&r={0}&n={1}&e={2}", Realm, Name, EventID)
            End Get
        End Property

        ''' <summary>
        ''' Indicates to the inherited class that this request requires login
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property RequiresLogin As Boolean
            Get
                Return True
            End Get
        End Property

        ''' <summary>
        ''' The summary of the event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Summary As String
            Get
                Return cdDetail.summary
            End Get
        End Property

        ''' <summary>
        ''' The event's calendar type
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property CalendarType As String
            Get
                Return cdDetail.calendarType
            End Get
        End Property

        ''' <summary>
        ''' The date of the event, in UTC
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property UTCStart As Date
            Get
                Return New Date(1970, 1, 1).AddMilliseconds(cdDetail.start)
            End Get
        End Property

        ''' <summary>
        ''' The type of event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property EventType As String
            Get
                Return cdDetail.type
            End Get
        End Property

        ''' <summary>
        ''' The owner of the event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Owner As String
            Get
                Return cdDetail.owner
            End Get
        End Property

        ''' <summary>
        ''' Indicates whether the event is locked or not
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Locked As Boolean
            Get
                Return cdDetail.locked
            End Get
        End Property

        ''' <summary>
        ''' The timezone offset of the event's server
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property TimezoneOffset As TimeSpan
            Get
                Return New TimeSpan(0, 0, 0, 0, CInt(cdDetail.tz))
            End Get
        End Property

        ''' <summary>
        ''' The date of the event, in local server time
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Start As DateTime
            Get
                Return New Date(1970, 1, 1).AddMilliseconds(cdDetail.start).AddMilliseconds(cdDetail.tz)
            End Get
        End Property

        ''' <summary>
        ''' The icon of the event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Icon As String
            Get
                Return cdDetail.icon
            End Get
        End Property

        ''' <summary>
        ''' The description of the event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Description As String
            Get
                Return cdDetail.description
            End Get
        End Property

        ''' <summary>
        ''' The location of the event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Location As String
            Get
                Return cdDetail.location
            End Get
        End Property

        ''' <summary>
        ''' Determines if this toon is the moderator of this event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Moderator As Boolean
            Get
                Return cdDetail.moderator
            End Get
        End Property

        ''' <summary>
        ''' The list of invitees for the event
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Invitees As Collection(Of Invitee)
            Get
                Return iInvitees
            End Get
        End Property

        ''' <summary>
        ''' A constructor to create new event information
        ''' </summary>
        ''' <param name="strUsername">The World of Warcraft/battle.net account username</param>
        ''' <param name="strPassword">The World of Warcraft/battle.net account password</param>
        ''' <param name="strRealm">The realm of the character for the calendar to lookup</param>
        ''' <param name="strName">The name of the character for the calendar to lookup</param>
        ''' <param name="intID">The identification number of the event</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal strUsername As String, ByVal strPassword As String, ByVal strRealm As String, ByVal strName As String, ByVal intID As Integer)
            Username = strUsername
            Password = strPassword
            Realm = strRealm
            Name = strName
            EventID = intID
            Load()
        End Sub

        ''' <summary>
        ''' Loads and deserialzes the JSON data
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Load()
            MyBase.Retrieve()

            Data = Regex.Match(Data, "calendar.loadEventDetail\((.*)\);").Groups(1).Value

            cdDetail = CType(JsonConvert.DeserializeObject(Data, GetType(Schema.calendarDetail)), Schema.calendarDetail)

            iInvitees.Clear()

            If cdDetail Is Nothing Then
                Throw New ArgumentException("The event ID did not return any information.")
                Exit Sub
            End If

            If cdDetail.invites Is Nothing Then
                Exit Sub
            End If

            For Each iInvite As Schema.invite In cdDetail.invites
                iInvitees.Add(New Invitee(CType(iInvite.classId, Enums.ClassType), CType(iInvite.genderId, Enums.GenderType), iInvite.invitee, iInvite.id, iInvite.moderator, CType(iInvite.raceId, Enums.RaceType), iInvite.status))
            Next
        End Sub

    End Class

End Namespace